import { Component, ContentChild, ElementRef, forwardRef, OnInit, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { CustomProps } from './custom-input.prop';
@Component({
  selector: 'app-custom-input',
  templateUrl: './custom-input.component.html',
  styleUrls: ['./custom-input.component.scss'],
  encapsulation: ViewEncapsulation.None,
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => CustomInputComponent),
      multi: true
    }
  ]
})
export class CustomInputComponent extends CustomProps implements OnInit, ControlValueAccessor {
  @ContentChild('prepend') prepend: ElementRef<any>;
  @ContentChild('append') append: ElementRef<any>;
  constructor() {
    super();
  }

  ngOnInit(): void {
  }


  handelInput(val) {
    this.model = val;
    this.modelChange.emit(val);
  }


  writeValue(value: any): void {
    this.model = value
  }
  
  registerOnChange(fn: Function): void {
    this.controlChange = fn
  }
  
  registerOnTouched(fn: Function): void {
    this.controlTouch = fn
  }
  
  private controlChange: Function = () => {}
  private controlTouch: Function = () => {}

}
